package com.tg.app.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tg.app.R;
import com.tange.base.toolkit.WifiAp;


import java.util.ArrayList;

public class DeviceWifiAdapter extends BaseAdapter {
    private ArrayList<WifiAp> list;
    private Context context;
    private OnAddWifiDeviceClickListener listener;
    private int selectPosition = -1;
    private SparseArray<ImageView> viewSparseArray;

    public DeviceWifiAdapter(ArrayList<WifiAp> list, Context context, OnAddWifiDeviceClickListener listener) {
        this.list = list;
        this.context = context;
        this.listener = listener;
        viewSparseArray = new SparseArray<>();
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


        @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.device_wifi_list_item, null);
            convertView.setTag(holder);
            holder.deviceWifiRl = convertView.findViewById(R.id.device_wifi_rl);
            holder.deviceWifiName = convertView.findViewById(R.id.device_wifi_name_text_view);
            holder.wifiLockImageView = convertView.findViewById(R.id.device_wifi_lock_image_view);
            holder.wifiSignalImageView = convertView.findViewById(R.id.device_wifi_signal_image_view);
            holder.wifiSelectorImageView = convertView.findViewById(R.id.imageButton2);
            viewSparseArray.put(position,holder.wifiSelectorImageView);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
            WifiAp  sWifiAp = list.get(position);

        if (sWifiAp.status ==1){
            holder.wifiSelectorImageView.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_yes);
        }else {
            holder.wifiSelectorImageView.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_no);
        }
        holder.deviceWifiRl.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
        holder.deviceWifiName.setText(sWifiAp.ssid);

        if (sWifiAp.enctype ==1) {
            holder.wifiLockImageView.setVisibility(View.INVISIBLE);
        } else {
            holder.wifiLockImageView.setVisibility(View.VISIBLE);
        }
        if (sWifiAp.signal >= 70) {
            holder.wifiSignalImageView.setImageResource(R.mipmap.wifi_strong);
        } else if (sWifiAp.signal > 40) {
            holder.wifiSignalImageView.setImageResource(R.mipmap.wifi_middle);
        } else {
            holder.wifiSignalImageView.setImageResource(R.mipmap.wifi_low);
        }

        return convertView;
    }

    public final class ViewHolder {
        TextView deviceWifiName;
        RelativeLayout deviceWifiRl;
        ImageView wifiLockImageView, wifiSignalImageView, wifiSelectorImageView;
    }

    public interface OnAddWifiDeviceClickListener {
        void onItemClick(int position);
    }
}
